import Vue from 'vue'
import Vuex from 'vuex'
import {
  getClassifygood,
  getGoods,
  getClassify,
  getGooddetail,
  postShoppingcart,
  getShoppingcart,
  deleteShoppingcart,
  updateShoppingcart,
  getOrder,
  getCity,
  getAddress,
  postAddress,
  postOrder,
  delectAddress,
  putAddress
} from "../api/index"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    navs: [],
    products: [],
    cls: [],
    detail: {},
    shopcart: {},
    city:[],
    address:[],
    order:[]
  },
  getters: {},
  mutations: {
    classify(state, val) {
      console.log(1);
      state.navs = val;
    },
    goods(state, val) {
      state.products = val;
    },
    classifyGoods(state, val) {
      state.cls = val
    },
    goodDetail(state, val) {
      state.detail = val
    },
    shoppingCart(state, val) {
      state.shopcart = val
    },
    city(state, val){
      state.city = val
    },
    address(state, val){
      state.address = val
    },
    order(state, val){
      state.order= val
    },
  },
  actions: {
    classify(content) {
      return getClassify()
        .then((res) => {
          // content.state.navs = res.result;
          content.commit("classify", res.result)
          console.log('分类', res.result);
        })
    },
    goods(content, params = {
      params: {
        page: 1,
        limit: 10,
      }
    }) {
      return getGoods(params)
        .then((res) => {
          console.log("商品", res.result.rows);
          content.commit("goods", res.result)
        })
    },
    classifyGoods(content) {
      return getClassifygood()
        .then(res => {
          content.commit("classifyGoods", res.result)
          console.log('分类商品', res.result, content.state.cls);
        })
    },
    goodDetail(content, id) {
      return getGooddetail(id).then(res => {
        content.commit("goodDetail", res.result)
        console.log(content, '详情', res);
      })
    },
    addshoppingCart(content, params) {
      return postShoppingcart(params).then(res => {
        console.log(content, "加入购物车", res);
      })
    },
    shoppingCart(content, params) {
      return getShoppingcart(params).then(res => {
        console.log(content, "购物车", res.result);
        content.commit("shoppingCart", res.result)
      })
    },
    deleteshoppingCart(content, AllId) {
      console.log('id', AllId);
      if (AllId.length) {
        AllId.forEach(el => {
          deleteShoppingcart(el).then(res => {
            content.dispatch('shoppingCart')
            console.log(content, "删除购物车", res, );
          })
        });
      }
    },
    updateshoppingCart(content, {
      id,
      num
    }) {
      console.log("数目", {
        num
      });
      updateShoppingcart(id, {
        num
      }).then(res => {
        content.dispatch('shoppingCart')
        console.log(content, "更新购物车", res, );
      })
    },
    order(content) {
      return getOrder()
        .then(res => {
          console.log('订单', res.result.rows, content);
          content.commit("order",res.result.rows)
        })
    },
    city(content){
      return getCity()
        .then(res => {
          console.log('城市', res.result);
          content.commit("city",res.result)
        })
    },
    address(content) {
      return getAddress()
        .then(res => {
          console.log('地址', res.result, content);
          content.commit("address",res.result)
        })
    },
    addAddress(content,data){
      return postAddress(data)
      .then(res => {
        console.log(res, content);
        // content.commit("classifyGoods",res.result)
      })
    },
    addOrder(content,data){
      return postOrder(data)
      .then(res => {
        console.log(res, content);
        // content.commit("classifyGoods",res.result)
      })
    },
    deleteaddress(content, id) {
      console.log('id', id);
      if (id) {
        delectAddress(id).then(res => {
        console.log(content, "删除购物车", res, );
        content.dispatch('address')
        });
      }
    },
    changeaddress(content, data){
      putAddress(data).then(res=>{
        console.log('修改成功',content,res);
      })
    }
  },
  modules: {}
})